<?php
namespace app\index\controller;

class Index extends Base{
    //首页
    public function index(){
    	$data = db('picture')->where('status=99')->order('id desc')->limit(10)->select();
    	$seo = $this->site();
    	$this->assign('data', $data);
    	$this->assign('seo', $seo);
      return $this->fetch();
    }
    //列表页
    public function cat(){
    	$catid = input('param.catid');
    	$where = "catid='$catid' and status=99";
    	$list = db('picture')->where($where)->order('id desc')->paginate(10,
    		false,[
            'page' => input('param.p'),
            'path'=> '/cat/'.$catid.'/p/[PAGE].html',
        ]);
    	$page = $list->render();
    	$cat = $this->category();
        $catname = $cat[$catid];
    	$seo = $this->site();
    	$seo['site_title'] = $catname.' - '.$seo['site_title'];
    	$this->assign('list', $list);
    	$this->assign('page', $page);
    	$this->assign('seo', $seo);
    	return $this->fetch();
    }
    //内容页
    public function detail(){
        $id = input('param.id');
        $detail = db('picture')->where("id='$id'")->find();
        $detail_data = db('picture_data')->where("id='$id'")->find();
        $data = array_merge($detail,$detail_data);
        $seo = $this->site();
        $seo['site_title'] = $data['title'].' - '.$seo['name'];
        $seo['keywords'] = str_replace(' ', ',', $data['keywords']);
        $data['pictureurls'] = json_decode($data['pictureurls'], true);
        $map['catid'] = $data['catid'];
        $map['posids'] = 1;
        $rec = db('picture')->where($map)->order('id desc')->limit(10)->select();
        //$hot = db('picture')->alias('a')->join('hits b', "concat('c-3-',a.id) = b.hitsid")->where('a.catid = '.$data['catid'])->order('b.views desc')->limit(10)->select();
        $cat = $this->category();
        $catname = $cat[$data['catid']];
        $pre_data = db('picture')->where("id < '$id' and catid=".$data['catid'])->order('id desc')->find();
        $next_data =  db('picture')->where("id > '$id' and catid=".$data['catid'])->order('id asc')->find();
        $this->assign('seo', $seo);
        $this->assign('data', $data);
        $this->assign('rec', $rec);
        //$this->assign('hot', $hot);
        $this->assign('catname', $catname);
        $this->assign('pre', $pre_data);
        $this->assign('next', $next_data);
        return $this->fetch();
    }
    //首页ajax下拉加载
    public function ajaxPic(){
    	$p = $_POST['page'] ? intval($_POST['page']) : 1;
    	$pagebase = 10;    //首页默认显示10张，这里基数为10
    	$pagenum = 6;      //ajax每次追加显示6张
    	$limit = $pagebase + ($p * $pagenum);
    	$count = db('picture')->where('status=99')->count();
    	$totalpage = ceil($count/$pagenum);
    	$list = db('picture')->where('status=99')->order('id desc')->limit($limit,$pagenum)->select();
    	if($totalpage > 1 && $p <= 10){
    		$data = array();
    		$data['code'] = 1;
  			$data['msg'] = 'OK';
  			if($totalpage > 10){
    			$data['pagetotal'] = 10;
  			}else{
    			$data['pagetotal'] = $totalpage;
  			}
  			foreach ($list as $k => $v) {
  				$list[$k]['inputtime'] = date('Y-m-d',$v['inputtime']);
  			}
  			$data['list'] = $list;
    	}else{
  			$data['code'] = 0;
  			$data['msg'] = '数据获取失败，请稍后再试！';
    	}
    	echo json_encode($data, JSON_FORCE_OBJECT);
    }
    //发送内容页图片
    public function detailImg(){
        $id = input('param.id');
        $detail = db('picture_data')->where("id='$id'")->find();
        $piclist = $detail['pictureurls'];
        echo $piclist;
    }
    //添加喜欢
    public function addEnjoy(){
        $id = input('param.id');
        $status = db('picture')->where("id='$id'")->setInc('digg');
        if($status != 0){
          echo "谢谢您的参与！";
        }else{
          echo "数据错误，请稍后重试！";
        }
    }
}
